package com.songlanyun.modules.goodsuserdiscount.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.goodsuserdiscount.entity.GoodsUserDiscountDetail;
import com.songlanyun.modules.goodsuserdiscount.model.dto.GoodsUserDiscountConfigDTO;
import com.songlanyun.modules.goodsuserdiscount.service.GoodsUserDiscountDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.goodsuserdiscount.dao.GoodsUserDiscountConfigDao;
import com.songlanyun.modules.goodsuserdiscount.entity.GoodsUserDiscountConfig;
import com.songlanyun.modules.goodsuserdiscount.service.GoodsUserDiscountConfigService;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsUserDiscountConfigService")
public class GoodsUserDiscountConfigServiceImpl extends ServiceImpl<GoodsUserDiscountConfigDao, GoodsUserDiscountConfig> implements GoodsUserDiscountConfigService {


    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsUserDiscountDetailService goodsUserDiscountDetailService;

    /**
     * 给商品设置自定义的会员等级折扣
     * @param shopId
     * @param goodsId
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public GoodsUserDiscountConfig set(Long shopId, Long goodsId, GoodsUserDiscountConfigDTO dto) {
        Goods goods = goodsService.getByShopIdAndId(shopId, goodsId);
        dto.setGoodsId(goods.getId());
        dto.getDetails().forEach(o->o.setGoodsId(goodsId));
        GoodsUserDiscountConfig config = this.getByGoodsId(goodsId);
        if(config==null)
        {
            GoodsUserDiscountConfig obj = new GoodsUserDiscountConfig();
            BeanUtils.copyProperties(dto,obj);
            this.create(obj);
            this.batchDetails(goodsId,dto);
            return obj;
        }
        else
        {
            Long id = config.getId();
            BeanUtils.copyProperties(dto,config);
            config.setId(id);
            this.modify(config);
            this.batchDetails(goodsId,dto);
            return config;
        }
    }


    private void batchDetails(Long goodsId, GoodsUserDiscountConfigDTO dto)
    {
        List<GoodsUserDiscountDetail> collect = dto.getDetails().stream().map(o -> {
            GoodsUserDiscountDetail detail = new GoodsUserDiscountDetail();
            BeanUtils.copyProperties(o, detail);
            detail.setGoodsId(goodsId);
            return detail;
        }).collect(Collectors.toList());
        goodsUserDiscountDetailService.batchDetail(goodsId,collect);
    }


    /**
     *  根据商品获取会员折扣配置
     * @param goodsId
     * @return
     */
    @Override
    public GoodsUserDiscountConfig getByGoodsId(Long goodsId) {
        GoodsUserDiscountConfig config = this.getOne(new LambdaQueryWrapper<GoodsUserDiscountConfig>()
                .eq(GoodsUserDiscountConfig::getGoodsId, goodsId)
                .last("limit 1"));
        return config;
    }


    private GoodsUserDiscountConfig create(GoodsUserDiscountConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    private GoodsUserDiscountConfig modify(GoodsUserDiscountConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }


}